package reduce;

import java.io.IOException;
import java.math.BigDecimal;

import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import plagiarismChecker.MapReduceSkeleton;

public class ReduceJob4 extends Reducer<Text, Text, Text, Text> {

    @Override
    protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
    	BigDecimal dotProduct = new BigDecimal("0");
    	BigDecimal euclideanNorm = new BigDecimal("0");
    	for(Text value:values){
    		String[] valueString = value.toString().split(MapReduceSkeleton.separator);
    		BigDecimal dotValue = new BigDecimal(valueString[0]);
    		dotProduct = dotProduct.add(dotValue);
    		euclideanNorm = new BigDecimal(valueString[1]);
    	}
		
    	Double result = Math.acos(dotProduct.doubleValue()/euclideanNorm.doubleValue());
    	
    	context.write(key, new Text(result.toString()));
    }
}